package com.bailan.controller;

import com.bailan.pojo.DTO.StudentMessageDTO;
import com.bailan.pojo.Message;
import com.bailan.pojo.VO.MessageVO;
import com.bailan.pojo.VO.StudentMessageVO;
import com.bailan.pojo.VO.TeacherMessageVO;
import com.bailan.service.IAdminService;
import com.bailan.service.IMessageService;
import com.bailan.service.ITeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BaiLan
 * @since 2022:05:24
 */
@RestController
@RequestMapping("/message")
public class MessageController {

    private IMessageService iMessageService;
    private IAdminService adminService;
    private ITeacherService teacherService;

    @Autowired
    public void setAdminService(IAdminService adminService) {
        this.adminService = adminService;
    }
    @Autowired
    public void setTeacherService(ITeacherService teacherService) {
        this.teacherService = teacherService;
    }

    @Autowired
    public void setiMessageService(IMessageService iMessageService) {
        this.iMessageService = iMessageService;
    }

    @RequestMapping(value = "/data")
    @ResponseBody
    public List<Message> getNoticeInformation(@RequestParam() Integer classId){
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("C_id",classId);
        List<Message> list = iMessageService.list(queryWrapper);

        return list;
    }


    /**
     * 获取全部通知消息
     * 适用于admin
     * @param index 页数
     * @param role 角色
     * @param need 需要
     * @return 页面
     */
    @GetMapping
    @ResponseBody
    public Page<Message> getAllInfo(@RequestParam("page") int index, @RequestParam("role") int role,@RequestParam("need") int need){
        Page<Message> page=new Page<>(index,need);

        LambdaQueryWrapper<Message> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        switch (role){
            case 1:lambdaQueryWrapper.select(
                    Message::getMTime,
                    Message::getFName,
                    Message::getMTitle,
                    Message::getMContent);
                break;
            case 2:lambdaQueryWrapper.select(
                    Message::getCId,
                    Message::getMTime,
                    Message::getFName,
                    Message::getMTitle,
                    Message::getMContent);
                break;
            case 3:lambdaQueryWrapper.select(
                    Message::getCId
                    ,Message::getMTime
                    ,Message::getFId
                    ,Message::getMTitle
                    ,Message::getMContent);
                break;
        }

        iMessageService.page(page,lambdaQueryWrapper);
        return page;
    }


    /**
     * 插入通知消息
     * 适用于admin teacher
     * @param messageVO 待消息视图
     * @return true | false
     */
    @PostMapping
    public boolean insertMessage(@RequestBody MessageVO messageVO){
        System.out.println(messageVO);
        String content = messageVO.getContent();
        String classNo = messageVO.getClassNo();
        String count = messageVO.getCount();
        String title = messageVO.getTitle();

        String name =iMessageService.getName(count);
        if (name==null){
            return false;
        }


        Message message = new Message();

        message.setCId(classNo);
        message.setMContent(content);
        message.setMTime(LocalDateTime.now().toString());
        message.setMTitle(title);
        message.setFId(count);
        message.setFName(name);


        return iMessageService.save(message);
    }


    /**
     * 删除通知消息
     * 适用于admin
     * @param id 消息id
     * @return true | false
     */
    @DeleteMapping
    public boolean deleteMessage(@RequestBody String id){
        return iMessageService.removeById(id);
    }

    @GetMapping("/student")
    @ResponseBody
    public StudentMessageDTO getInfoForStudent(@RequestBody StudentMessageVO messageVO){
        System.out.println(messageVO);
        Page<Message> messagePage = new Page<>(messageVO.getCurrent(),messageVO.getNeed());

        LambdaQueryWrapper<Message> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        String classId = iMessageService.getStudentClassId(messageVO.getStudentId());

        lambdaQueryWrapper.in(Message::getCId,classId);

        Page<Message> page = iMessageService.page(messagePage, lambdaQueryWrapper);

        StudentMessageDTO studentMessageDTO = new StudentMessageDTO();
        int current = (int)page.getCurrent();
        int size = (int) page.getSize();
        studentMessageDTO.setCurrent(current);
        studentMessageDTO.setNeed(size);
        studentMessageDTO.setTotal((int) page.getTotal());
        studentMessageDTO.setRecords(page.getRecords());

        return studentMessageDTO;
    }

    @GetMapping("/teacher")
    @ResponseBody
    public StudentMessageDTO getInfoForTeacher(@RequestBody TeacherMessageVO messageVO){
        System.out.println(messageVO);
        Page<Message> messagePage = new Page<>(messageVO.getCurrent(),messageVO.getNeed());

        LambdaQueryWrapper<Message> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        List<String> classId =iMessageService.getTeacherClassId(messageVO.getTeacherId());

        System.out.println(classId.toString());

        lambdaQueryWrapper.in(Message::getCId,classId);

        Page<Message> page = iMessageService.page(messagePage, lambdaQueryWrapper);



        StudentMessageDTO studentMessageDTO = new StudentMessageDTO();
        int current = (int)page.getCurrent();
        int size = (int) page.getSize();
        studentMessageDTO.setCurrent(current);
        studentMessageDTO.setNeed(size);
        studentMessageDTO.setTotal((int) page.getTotal());
        studentMessageDTO.setRecords(page.getRecords());

        return studentMessageDTO;

    }
}
